package com.hevs.adibilis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CategoriesActivity extends Activity{

	ListView listView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categories);

		this.listView = (ListView) findViewById(R.id.listViewCategories);

		
		if(Utils.hasInternet(this)){
			new DownloadDataFromWeb().execute();
		} else {
			Toast.makeText(this, R.string.PleaseConnectToInternet, Toast.LENGTH_LONG).show();
		}


	}


	// Uses AsyncTask to create a task away from the main UI thread. This task takes a 
	// URL string and uses it to create an HttpUrlConnection. Once the connection
	// has been established, the AsyncTask downloads the contents of the webpage as
	// an InputStream. Finally, the InputStream is converted into a string, which is
	// displayed in the UI by the AsyncTask's onPostExecute method.
	private class DownloadDataFromWeb extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {


		// onPostExecute displays the results of the AsyncTask.
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

			
				 /**
		         * Updating parsed JSON data into ListView
		         * */
		        ListAdapter adapter = new SimpleAdapter(getApplicationContext(), result,
		                R.layout.list_item_category,
		                new String[] { JSONFactory.TAG_ID, JSONFactory.TAG_NAME }, new int[] {
		                        R.id.idCategorieTextView, R.id.nameCategorieTextView });
		 
		        listView.setAdapter(adapter); //
		        listView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						
				         // getting values from selected ListItem
		                String id_activity = ((TextView) view.findViewById(R.id.idCategorieTextView)).getText().toString(); //TODO chercher dans le hash
		                String name = ((TextView) view.findViewById(R.id.nameCategorieTextView)).getText().toString();

		                // Starting new intent
		                Intent in = new Intent(getApplicationContext(), ActivitiesActivity.class);
		                in.putExtra(JSONFactory.TAG_ID, id_activity);
		                in.putExtra(JSONFactory.TAG_NAME, name);
		                startActivity(in);
						
					}
		        
		        });



		}

		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... idString) {


			try {
				JSONFactory.getCategories();
				return JSONFactory.parseCategories();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoNodesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}


}
